import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;


/*
 * URL重写
 * */
@WebServlet("/e")
public class ServletE extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取或创建一个新的会话
        HttpSession session = request.getSession(true);
        session.setAttribute("s1","aaaaa");

        // 假设我们有一个目标URL
        String targetUrl = request.getContextPath()+"/f";

        // 使用response对象的方法进行URL重写，将session ID添加到URL中，用于重定向
        String encodedUrl = response.encodeRedirectURL(targetUrl);
        System.out.println(session.getId());
        // 发送重定向响应，其中包含了会话ID
        response.sendRedirect(encodedUrl);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}